<template>
  <div class="login">
    <div class="main">
      <div class="left-box">
        <img src="@/assets/logo/logo.png" alt="" class="img1" />
        <!-- <img src="@/assets/image/img_bg.png" alt="" class="img2" /> -->
        <h3>烈阳光伏管理系统</h3>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <h2 class="title">欢迎登录</h2>
        <el-form-item prop="deptId">
          <el-select
            v-model="loginForm.deptId"
            placeholder="请选择所属公司"
            style="width: 100%"
            @change="changelist"
          >
            <el-option
              v-for="item in companyOptions"
              :key="item.deptId"
              :label="item.name"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            @focus="isUsername = true"
            @blur="isUsername = false"
            placeholder="用户名"
          >
            <svg-icon
              slot="prefix"
              icon-class="user"
              v-bind:class="{ 'color-class': isUsername }"
              class="input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @focus="isPassword = true"
            @blur="isPassword = false"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon
              slot="prefix"
              icon-class="password"
              v-bind:class="{ 'color-class': isPassword }"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row :gutter="10">
            <el-col :span="14">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                @focus="isValidCode = true"
                @blur="isValidCode = false"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="validCode"
                  v-bind:class="{
                    'color-class': isValidCode,
                  }"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-col>
            <el-col :span="10">
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" />
              </div>
            </el-col>
          </el-row>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin: 0 0 10px 0"
          >记住密码
        </el-checkbox>
        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 100%"
            @click.native.prevent="handleLogin"
            round
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-login-footer"></div>
  </div>
</template>

<script>
import { getCodeImg, getDept } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      isUsername: false,
      isPassword: false,
      isValidCode: false,
      codeUrl: "",
      cookiePassword: "",
      companyOptions: [],
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
        deptId: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: "blur",
            message: "用户名不能为空",
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            message: "密码不能为空",
          },
        ],
        code: [
          {
            required: true,
            trigger: "change",
            message: "验证码不能为空",
          },
        ],
        deptId: [
          {
            required: true,
            trigger: "change",
            message: "所属公司不能为空",
          },
        ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.getCode();
    this.getCookie();
    this.getDept();
  },
  methods: {
    changelist(val) {
      console.log(val);
      for (let i = 0; i < this.companyOptions.length; i++) {
        if (this.companyOptions[i].deptId == val) {
          sessionStorage.setItem("userName", this.companyOptions[i].name);
          sessionStorage.setItem("userCode", this.companyOptions[i].deptId);
        }
      }
    },
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const deptId = Cookies.get("deptId");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        deptId: deptId === undefined ? this.loginForm.deptId : deptId,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),

        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {
              expires: 30,
            });
            Cookies.set("deptId", this.loginForm.deptId, {
              expires: 30,
            });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("deptId");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
    async getDept() {
      let res = await getDept();
      this.companyOptions = res.data;
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: url("../assets/image/login_bg.jpg");
  background-position: center;
  background-size: 100% 100%;
  background-repeat: none;
  position: relative;
}

::v-deep .el-form-item {
  margin-bottom: 30px;
}

::v-deep .el-input__inner {
  border-radius: 20px;
}

.main {
  position: relative;
  width: 50%;
  height: 600px;
  border-radius: 20px;
  // box-shadow: 0 2px 12px 0 rgba(9, 23, 39, 0.3);
  color: rgba(255, 255, 255, 0.8);
  // left: -6%;
  display: flex;
  // padding: 20px;
  .img1 {
    width: 110px;
  }

  .img2 {
    position: absolute;
    top: 63px;
    left: 132px;
    width: 348px;
    height: 299px;
  }

  .left-box {
    padding: 40px;
    width: 68%;
    height: 100%;
    background: url("../assets/image/login_bg.png");
    background-size: 100% 100%;
  }

  h3 {
    font-weight: 300;
    letter-spacing: 6px;
    text-indent: 5px;
    font-size: 20px;
    margin: 8px 0;
  }

  h4 {
    margin-left: 52px;
    font-size: 24px;
    margin-top: 16px;
  }

  .login-form {
    // position: absolute;
    right: -25%;
    top: 10%;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
    background: #ffffff66;
    width: 360px;
    height: 100%;
    padding: 50px;

    .title {
      width: 85px;
      font-size: 20px;
      margin: 40px auto 40px auto;
      // border-bottom: 3px rgba(27, 74, 127, 1) ridge;
      text-align: center;
      color: #333333;
      padding-bottom: 8px;
      position: relative;
    }

    .title:before {
      content: "";
      width: 40px;
      height: 2px;
      background: #92989c;
      position: absolute;
      left: -59%;
      top: 35%;
    }

    .title:after {
      content: "";
      width: 40px;
      height: 2px;
      background: #92989c;
      position: absolute;
      left: 110%;
      top: 35%;
    }

    .el-input {
      height: 32px;

      input:focus {
        color: rgba(6, 104, 185, 1);
      }

      input {
        outline: none;
        height: 38px;
        border-top: 0;
        border-left: 0;
        border-right: 0;
        // border-radius: 0;
        border-radius: 20px;
        border: 1px solid #eee;
      }
    }

    .color-class {
      color: rgba(6, 104, 185, 1);
    }

    .input-icon {
      height: 32px;
      width: 19px;
      margin-left: 2px;
    }
  }
}

.login-code {
  position: absolute;
  width: 88px;
  height: 32px;
  right: 0;
  bottom: 5px;

  img {
    width: 88px;
    height: 32px;
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: absolute;
  transform: translateY(273px);
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;

  span {
    img {
      height: 12px;
    }
  }
}

@media screen and (max-width: 1500px) {
  .main {
    height: 90%;
  }
}
</style>
